package com.bbx.taxi.client.DB.MyOrder;

import java.util.HashMap;
import java.util.Map;

import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class OrderCencelDB implements DBcolumns {
	private DBHelper helper;

	public OrderCencelDB() {
		MyApplication myApplication=MyApplication.getInstance();
		helper = DBHelper.getInstance(myApplication.getApplicationContext(),myApplication.getUid());
	}

	/**
	 * 打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() {
		if(helper==null){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
		}
		SQLiteDatabase SQLdb ;
		try{SQLdb= helper.getReadableDatabase();}catch(Exception e){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
			SQLdb= helper.getReadableDatabase();
		}
		return SQLdb;
	}

	/**
	 * 打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() {
		if(helper==null){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
		}
		SQLiteDatabase SQLdb ;
		try{SQLdb= helper.getReadableDatabase();}catch(Exception e){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
			SQLdb= helper.getReadableDatabase();
		}
		return SQLdb;
	}

	/**
	 * 查询
	 */
	public Cursor onQuery() {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_ORDER_CANCEL, null, null, null, null,
				null, CENCEL_ID + " desc");
		return cursor;

	}

	/**
	 * 根据订单id查询
	 */
	public Cursor onQuery(String order_id) {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = SQLdb
				.query(TABLE_ORDER_CANCEL, null, CENCEL_ORDER_ID + "='"
						+ order_id + "'", null, null, null, CENCEL_ID
						+ " desc ");
		return cursor;
	}

	/**
	 * 删除
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb = onOpenWrite();
		SQLdb.delete(TABLE_ORDER_CANCEL, null, null);
		SQLdb.close();
	}

	/**
	 * 根据Id删除
	 */
	public void onDelete(String rowId) {
		SQLiteDatabase SQLdb = onOpenWrite();
		String[] whereArgs = { String.valueOf(rowId) };
		int i = SQLdb.delete(TABLE_ORDER_CANCEL, CENCEL_ID + "=?", whereArgs);
		SQLdb.close();
	}

	/**
	 * 插入一条数据 ,collection为1则代表收藏，0为历史
	 */
	public void onWrite(String order_id, String reason) {
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues values = new ContentValues();
		values.put(CENCEL_ORDER_ID, order_id);
		values.put(CANCEL_REASON, reason);
		SQLdb.insert(TABLE_ORDER_CANCEL, CENCEL_ID, values);
		SQLdb.close();
	}

	/**
	 * 获取原因
	 */
	public String onGetReason(String order_id) {
		String reason = "";
		Cursor c = onQuery(order_id);
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				reason=map.get(DBcolumns.CANCEL_REASON);
			} while (c.moveToNext());
		}
		c.close();
		return reason;
	}

	/**
	 * 关闭数据库
	 */
	public void onColse() {
		SQLiteDatabase SQLdb = onOpenWrite();
		SQLdb.close();
	}
}
